/**
 * @author Luc Yriarte
 * 
 * Licence : BSD http://www.opensource.org/licenses/bsd-license.php
 *  
 */
package basicSim;

/**
 * Box with its length on z, width on x, height on y
 * factor fwidth != 1  makes a flat trapezoid
 */

public class Box extends Object3D {

	double length, width, height, fwidth;

	/**
	 * @param x width
	 * @param y height
	 * @param z length
	 */
	public void init(double x, double y, double z, double fx) {
		width = x;
		height = y;
		length = z;
		fwidth = fx;
		double dx = x / 2;
		double dy = y / 2;
		double dz = z / 2;
		double dbx = dx * fwidth;
		Vector3D v[] = new Vector3D[8];
		// bottom rectangle
		v[0] = new Vector3D(-dbx, -dy, -dz);
		v[1] = new Vector3D(-dbx, -dy, dz);
		v[2] = new Vector3D(dbx, -dy, dz);
		v[3] = new Vector3D(dbx, -dy, -dz);
		// top rectangle
		v[4] = new Vector3D(-dx, dy, -dz);
		v[5] = new Vector3D(-dx, dy, dz);
		v[6] = new Vector3D(dx, dy, dz);
		v[7] = new Vector3D(dx, dy, -dz);
		nFacet = 6;
		facet = new PolygonEdgeIndexes[nFacet];
		// bottom rectangle
		facet[4] = new PolygonEdgeIndexes(this, 4);
		// top rectangle
		facet[5] = new PolygonEdgeIndexes(this, 4);
		int[][] e = new int[12][2];
		int i;
		for (i = 0; i < 4; i++) {
			// bottom rectangle
			e[i][0] = i;
			e[i][1] = (i + 1) % 4;
			// bottom rectangle facet
			facet[4].edge[i] = i;
			// top rectangle
			e[i + 4][0] = i + 4;
			e[i + 4][1] = ((i + 1) % 4) + 4;
			// top rectangle facet
			facet[5].edge[i] = i + 4;
			// vertical lines
			e[i + 8][0] = i;
			e[i + 8][1] = i + 4;
			// vertical rectangle facets
			facet[i] = new PolygonEdgeIndexes(this, 4);
			facet[i].edge[0] = i;
			facet[i].edge[1] = (i + 1) % 4;
			facet[i].edge[2] = ((i + 1) % 4) + 4;
			facet[i].edge[3] = i + 4;
		}
		mesh = new Mesh(8, 12, v, e);
	}

	public Box(double x, double y, double z) {
		super();
		init(x, y, z, 1);
	}

	public Box(double x, double y, double z, double fx) {
		super();
		init(x, y, z, fx);
	}

	public String strObject(int nTab) {
		String str = "", strTab = "";
		for (int i = 0; i < nTab; i++)
			strTab += "\t";
		str += strTab + "{ box  w=" + new Double(width).toString() + " h="
				+ new Double(height).toString() + " l="
				+ new Double(length).toString() + "\n";
		str += strCoord(nTab);
		str += strColor(nTab);
		str += strChild(nTab);
		str += strTab + "}\n";
		return str;
	}

}
